package com.web.mqtt.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.web.common.iot.mqtt.api.MqttLogApi;
import com.web.common.iot.mqtt.entity.MqttBaseListenerEntity;
import com.web.common.iot.mqtt.entity.MqttMessageType;
import com.web.common.result.R;
import com.web.mqtt.service.impl.MqttListenServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "MQTT消息、事件日志")
@Controller
@RequestMapping("/iot/mqtt/log")
public class MqttLogController implements MqttLogApi {
	
	@Autowired
	private MqttListenServiceImpl mqttListenServiceImpl;
	
	@Operation(summary = "获取MQTT消息类型列表")
	@PostMapping(path = "/type/list")
	@ResponseBody
	public R<List<Map<String, String>>> getMqttMessageType(){
		List<Map<String, String>> dataList = new ArrayList<>(MqttMessageType.values().length);
		for(MqttMessageType item: MqttMessageType.values()) {
			Map<String, String> dataItem = new HashMap<>();
			dataItem.put(item.getType(), item.getMessage());
			dataList.add(dataItem);
		}
		return R.ok(dataList);
	}

	@Operation(summary = "获取MQTT日志列表")
	@Override
	public R<IPage<MqttBaseListenerEntity>> getMqttLogList(MqttBaseListenerEntity condition) {
		if(condition.getStartDate() == null || condition.getEndDate() == null) {
			return R.fail(null, "请选择开始结束时间");
		}
		int year = condition.getStartDate().getYear() - condition.getEndDate().getYear();
		if(year > 1) {
			return R.fail(null, "查询数据范围不能超过一年");
		}
		return mqttListenServiceImpl.getMqttLogList(condition);
	}
}
